<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	<xsl:template match="response" mode="comments">
		<xsl:param name="userAvatar" select="$oUser/property[@name='avatar']/value" />
		<xsl:param name="userLogin" select="$oUser/property[@name='login']/value" />
		
		<div id="comments_list" class="comments-list">
			<xsl:apply-templates />
			
			<xsl:if test="$userGroup = 'guest'">
				<div class="simple-message">Для того, чтобы оставить комментарий <a href="{$baseURI}/users/login">авторизуйтесь</a> или <a href="{$baseURI}/users/registration">зарегистрирутесь</a>.</div>
			</xsl:if>
			<xsl:if test="$userGroup != 'guest'">
				<div id="write_comment" class="write-comment">
					<div class="owner">
						<div class="avatar">
							<a href="{$baseURI}/user/{$userLogin}">
								<xsl:if test="$userAvatar = ''">
									<img src="{$baseURI}/images/default-user-avatar.png" alt="{$userLogin}" />
								</xsl:if>
								<xsl:if test="$userAvatar != ''">
									<img src="{$baseURI}/thumb_50_50{$userAvatar}" alt="{$userLogin}" />
								</xsl:if>
							</a>
						</div>
					</div>
					<div class="body">
						<div class="name">
							<xsl:text>Вы — </xsl:text>
							<a href="{$baseURI}/user/{$userLogin}">
								<xsl:value-of select="$userLogin" />
							</a>
						</div>
						<form method="post" action="javascript:void(0);" onsubmit="Comment.post (0, '{@type-name}', {@object-id}); return false;">
							<textarea rows="4" id="comment_text_0"/>
							<div class="error" id="comment_error_0" style="display: none;">Вы должны сначала написать комментарий</div>
							<div class="buttons">
								<input type="submit" value="Комментировать"/>
							</div>
						</form>
					</div>
				</div>
			</xsl:if>
		</div>
	</xsl:template>
	
	<xsl:template match="object[@type='comment']">
		<xsl:param name="rating" select="property[@name='rating']/value" />
		<xsl:param name="userAvatar" select="property[@name='user_avatar']/value" />
		<xsl:param name="allowVote" select="@allow-vote" />
		<xsl:param name="userLogin" select="property[@name='user_login']/value" />
		
		<a name="comment{@id}" />
		<div class="item" id="comment_{@id}">
			<xsl:attribute name="style">
				<xsl:value-of select="concat ('margin-left: ', number(@comment-level) * 50, 'px')" />
			</xsl:attribute>
			
			<xsl:if test="property[@name='deleted']/value = 1">
				<xsl:attribute name="class">item deleted</xsl:attribute>
				<p>Комментарий удален</p>
			</xsl:if>
			
			<xsl:if test="property[@name='deleted']/value = 0">
				<div class="avatar">
					<a href="{$baseURI}/user/{$userLogin}">
						<xsl:if test="$userAvatar = ''">
							<img src="{$baseURI}/images/default-user-avatar.png" alt="{$userLogin}" />
						</xsl:if>
						<xsl:if test="$userAvatar != ''">
							<img src="{$baseURI}/thumb_50_50{$userAvatar}" alt="{$userLogin}" />
						</xsl:if>
					</a>
				</div>
				
				<div class="body">
					<div class="header">
						<xsl:apply-templates select="document(concat('xmodule://system/getVoted/', $rating, ';', $allowVote, ';', @type-id, ';', @id, ';0'))/data/response/vote" mode="comment-vote" />
						
						<a class="username" href="{$baseURI}/user/{$userLogin}">
							<xsl:value-of select="$userLogin" />
						</a>
						
						<span class="date">
							<xsl:value-of select="property[@name='pubdate']/value" />
						</span>
					</div>
					
					<div class="content">
						<xsl:value-of select="property[@name='text']/value" disable-output-escaping="yes" />
					</div>
					
					<xsl:if test="$userGroup = 'guest'">
						<div class="controls">
							<a href="#comment{@id}">ссылка</a>
						</div>
					</xsl:if>
					<xsl:if test="$userGroup != 'guest'">
						<div class="controls">
							<a href="#comment{@id}">ссылка</a>
							<a href="#" onclick="Comment.showForm ({@id}, '{../@type-name}', {../@object-id}); return false;">ответить</a>
							<xsl:if test="$userGroup = 'admin' or $userGroup = 'moderator'">
								<a href="#" onclick="if (confirm ('Вы действительно хотите удалить этот комментарий?')) Comment.delete ({@id}); return false;">удалить</a>
							</xsl:if>
						</div>
						<div class="reply" style="display:none;" id="new_comment_{@id}" />
					</xsl:if>
				</div>
			</xsl:if>
		</div>
	</xsl:template>
	
	<!-- VOTE -->
	<xsl:template match="vote" mode="comment-vote">
		<xsl:param name="id" select="object_id" />
		<xsl:param name="vote" select="mark" />
		<xsl:param name="allow" select="allow" />
		<xsl:param name="rating" select="rating" />
		<xsl:param name="format" select="format" />
		
		<div class="rating" id="comment_rating_{$id}">
			<a class="plus" href="javascript:void(0);" title="нравится">
				<xsl:choose>
					<xsl:when test="$allow = '0' and $vote = ''">
						<xsl:attribute name="onclick">new Error ('Вы не можете проголосовать');</xsl:attribute>
						<xsl:attribute name="class">plus cant</xsl:attribute>
					</xsl:when>
					<xsl:when test="$allow = '0' and $vote != ''">
						<xsl:attribute name="onclick">new Error ('Повторное голосование запрещено');</xsl:attribute>
						<xsl:if test="$vote = '1'">
							<xsl:attribute name="class">plus voted</xsl:attribute>
						</xsl:if>
						<xsl:if test="$vote != '1'">
							<xsl:attribute name="class">plus cant</xsl:attribute>
						</xsl:if>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="onclick">
							<xsl:value-of select="concat ('Comment.vote (', $id, ', 1);')" />
						</xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
				
				<xsl:text>+</xsl:text>
			</a>
			
			<span class="number">
				<xsl:if test="number ($rating) &gt; 0">
					<xsl:attribute name="class">number plus</xsl:attribute>
				</xsl:if>
				<xsl:if test="number ($rating) &lt; 0">
					<xsl:attribute name="class">number minus</xsl:attribute>
				</xsl:if>
				
				<xsl:value-of select="$format" />
			</span>
			
			<a class="minus" href="javascript:void(0);" title="не нравится">
				<xsl:choose>
					<xsl:when test="$allow = '0' and $vote = ''">
						<xsl:attribute name="onclick">new Error ('Вы не можете проголосовать');</xsl:attribute>
						<xsl:attribute name="class">minus cant</xsl:attribute>
					</xsl:when>
					<xsl:when test="$allow = '0' and $vote != ''">
						<xsl:attribute name="onclick">new Error ('Повторное голосование запрещено');</xsl:attribute>
						<xsl:if test="$vote = '0'">
							<xsl:attribute name="class">minus voted</xsl:attribute>
						</xsl:if>
						<xsl:if test="$vote != '0'">
							<xsl:attribute name="class">minus cant</xsl:attribute>
						</xsl:if>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="onclick">
							<xsl:value-of select="concat ('Comment.vote (', $id, ', 0);')" />
						</xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
				
				<xsl:text>−</xsl:text>
			</a>
		</div>
	</xsl:template>
	
</xsl:stylesheet>